%% 小区范围 1km，均有直射径
clc; clear;close all;

load("AF_dB.mat")
Gt_dB_narrow = 36.1;
% 参数设置
mu = 500;        % 均值
sigma = 200;     % 标准差
N = 5000;        % 仿真次数
targetNum = 5;   % 目标数
r = 100;         % 小区半径
posA = [0;0];posB = [r;0];
% 初始化
total_needed = N;
data = [];

% 生成高斯随机数，限制在 [1, r]
while length(data) < total_needed
    x = -r/2 + (r + r/2) * rand(1, 1);          % 均匀分布
    y = r * rand(1, 1);
    temp = [x;y];
    if norm(temp)<r && y>-1*sqrt(3)*x
        data = [data, temp];  
    end
end
target = data(:,1:total_needed);

figure;
scatter(target(1,:), target(2,:), 10, 'filled');  % 40 是点的大小
xlabel('X 坐标');
ylabel('Y 坐标');
title('二维坐标点分布');
grid on;
axis equal;  % 坐标轴比例一致（真实反映距离）

for i=1:N
    theta = atand(target(2,i)/target(1,i));
    if theta<0
        theta = 180-abs(theta);
    end
    theta0 = 60-theta;       % 换算为天线面板夹角
    Gt_dB_wide = AF_dB(round((theta0+90)/0.1+1));
    dist = norm(target(:,i))+norm(target(:,i)-posB);
    [Pr_dBm_wide(i),Pr_dBm2(i)] = linkbudget(dist,Gt_dB_wide,Gt_dB_wide,1);
    [Pr_dBm_narrow(i),~] = linkbudget(dist,Gt_dB_wide,Gt_dB_narrow,0,targetNum);
end
draw(Pr_dBm_wide,Pr_dBm_narrow,Pr_dBm2)